package org.jeecg.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhyd.oauth.utils.StringUtils;
import org.jeecg.modules.app.entity.Customer;
import org.jeecg.modules.app.mapper.CustomerMapper;
import org.jeecg.modules.app.service.CustomerService;
import org.jeecg.modules.app.vo.LoginCustomer;
import org.jeecg.modules.app.vo.RegisterCustomer;
import org.jeecg.modules.app.vo.RemakeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 李圳川
 * @version 1.0
 * @date 2022/6/9 20:32
 * @content
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * app顾客登录
     * @param loginCustomer
     * @return
     */
    @Override
    public Customer login(LoginCustomer loginCustomer) {

        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        Customer customer
                = customerMapper.selectOne(
                queryWrapper.eq("username",loginCustomer.getUsername().trim())
                        .eq("password",loginCustomer.getPassword().trim())
        );
        return customer;
    }

    /**
     * app顾客注册
     * @param registerCustomer
     * @return
     */
    @Override
    public Boolean register(RegisterCustomer registerCustomer) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        Customer customer
                = customerMapper.selectOne(
                queryWrapper.eq("username",registerCustomer.getUsername().trim()));
        if(customer != null){
            return false;
        }else{
            customer = new Customer();
            customer.setUsername(registerCustomer.getUsername().trim());
            customer.setPassword(registerCustomer.getPassword().trim());
            customerMapper.insert(customer);
            return true;
        }
    }

    /**
     * app获取顾客的登录信息
     * @param customerToken
     * @return
     */
    @Override
    public Customer getCustomerByToken(String customerToken) {


        if(StringUtils.isEmpty(customerToken)){
            return  null;
        }
        Customer customer = (Customer) redisTemplate.opsForValue().get("token:"+customerToken);
        return customer;
    }

    /**
     * 更新顾客信息
     * @param customer
     */
    @Override
    public void updateCustomer(Customer customer) {
        customerMapper.updateCustomerById(customer);
    }

    @Override
    public List<RemakeVo> selectCustomerRemakeById(String id) {
        List<RemakeVo> remakeVoList  = customerMapper.selectCustomerRemakeById(id);
        return remakeVoList;
    }
}
